package com.central.scheduler.api.controller;

import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.*;
import com.central.scheduler.api.service.IMasterServerService;
import com.central.scheduler.dao.model.MasterServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@RestController
@Api(tags = "master节点模块api")
@Slf4j
@RequestMapping("/master")
public class MasterController {
    @Autowired
    private IMasterServerService masterServerService;

    @ApiOperation(value = "查询master列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/findMasters")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public PageResult<MasterServer> findMasters(@RequestParam Map<String, Object> params) {
        return masterServerService.findMasters(params);
    }

    @ApiOperation(value = "修改master节点名称")
    @GetMapping("/updateName")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result updateName(Long masterId,String name) {
        try {
            MasterServer master = masterServerService.getById(masterId);
            master.setName(name);
            masterServerService.updateById(master);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            log.error("scheduler-updateNickname-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "修改master节点描述")
    @GetMapping("/updateDesc")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result updateDesc(Long masterId,String desc) {
        try {
            MasterServer master = masterServerService.getById(masterId);
            master.setDescription(desc);
            masterServerService.updateById(master);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            log.error("scheduler-updateDesc-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "删除master节点")
    @DeleteMapping("/delete")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result delete(Long masterId) {
        try {
            return masterServerService.removeByIdAndOff(masterId);
        } catch (Exception ex) {
            log.error("scheduler-master-delete-error", ex);
            return Result.failed("操作失败");
        }
    }
}
